package com.xdja.kafka.util;
import com.alibaba.fastjson.JSON;
import com.xdja.kafka.hdfs.sink.bean.HuaweiLoginBean;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class LoginUtil {
    protected static Logger logger = LoggerFactory.getLogger(LoginUtil.class);

    /**
     * 华为云标准认证方式
     * @param huaweiLoginBean
     * @throws IOException
     */
    public static void login(HuaweiLoginBean huaweiLoginBean) throws IOException {
        logger.info("开始进行华为云标准认证，获取的各项参数为， huaweiLoginBean：{}", JSON.toJSONString(huaweiLoginBean));
        Configuration hadoopConf = new Configuration();
        hadoopConf.set("hadoop.security.authentication", "kerberos");
//        hadoopConf.set("hadoop.rpc.protection", "authentication");

        hadoopConf.addResource(new Path(huaweiLoginBean.getHdfsSitePath()));
        hadoopConf.addResource(new Path(huaweiLoginBean.getCoreSitePath()));

        com.huawei.hadoop.security.LoginUtil.setJaasConf(huaweiLoginBean.getZkLoginContentName(), huaweiLoginBean.getUserPrincipal(), huaweiLoginBean.getUserKeytabPath());
        com.huawei.hadoop.security.LoginUtil.setZookeeperServerPrincipal(huaweiLoginBean.getZkServerPrincipalKey(), huaweiLoginBean.getZkServerPrincipal());
        com.huawei.hadoop.security.LoginUtil.login(huaweiLoginBean.getUserPrincipal(), huaweiLoginBean.getUserKeytabPath(), huaweiLoginBean.getKrb5ConfPath(), hadoopConf);
        logger.info("连接huawei hadoop标准认证通过");

    }
}
